﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using agvcontroller.Control;

namespace agvcontroller.GUI
{
    /// <summary>
    /// Interaction logic for WNDManualControl.xaml
    /// </summary>
    public partial class WNDManualControl : Window
    {
        CVehicleDic VehicleDic;
        CVehicle CurVehicle;

        public WNDManualControl()
        {
            InitializeComponent();

            VehicleDic = CVehicleDic.getInstance();

            InitUI();
            RegistEvent();
        }

        void InitUI()
        {
            if (VehicleDic.Count == 0) return;
            uCManualControl.SetVehicle(VehicleDic.Values.First<CVehicle>());

            CurVehicle = VehicleDic.Values.First<CVehicle>();
            uCExplorer.SetName("ManualControl");
            uCExplorer.SetCurItem(CurVehicle.Name);
            uCManualControl.SetVehicle(CurVehicle);
        }

        void RegistEvent()
        {
            uCExplorer.onPrev += new deleExplorerPre(uCExplorer_onPrev);
            uCExplorer.onNext += new deleExplorerNext(uCExplorer_onNext);
        }

        void uCExplorer_onNext(object sender)
        {
            if (((UCExplorer)sender).ExplorerName != "ManualControl") return;

            CVehicle nextVehicle = VehicleDic.GetNext(CurVehicle);

            if (nextVehicle == null) return;

            uCExplorer.SetCurItem(nextVehicle.Name);

            CurVehicle = nextVehicle;

            uCManualControl.SetVehicle(CurVehicle);
        }

        void uCExplorer_onPrev(object sender)
        {
            if (((UCExplorer)sender).ExplorerName != "ManualControl") return;

            CVehicle prevVehicle = VehicleDic.GetPrev(CurVehicle);

            if (prevVehicle == null) return;

            uCExplorer.SetCurItem(prevVehicle.Name);

            CurVehicle = prevVehicle;

            uCManualControl.SetVehicle(CurVehicle);
        }
        

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
